MyDesktop.ProductWindow = Ext.extend(Ext.app.Module, {
    id:'product-win',
    init : function(){
        this.launcher = {
            text: '产品管理',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },
    productRecord : Ext.data.Record.create([
	    {name : 'id',mapping : 'id',type : 'int'}, 
	    {name : 'name',mapping : 'name',type : 'string'}, 
		{name : 'pinyin',mapping : 'pinyin',type : 'string'}, 
	    {name : 'typeCode',mapping : 'typeCode',type : 'string'}, 
	    {name : 'brand',mapping : 'brand',type : 'string'}, 
	    {name : 'power',mapping : 'power',type : 'string'}, 
	    {name : 'functionName',mapping : 'functionName',type : 'string'}, 
	    {name : 'category',mapping : 'category',type : 'string'},
	    {name : 'unit',mapping : 'unit',type : 'string'},
	    {name : 'seer',mapping : 'seer',type : 'string'}
	 ]),
	cm_product : new Ext.grid.ColumnModel([
	       new Ext.grid.RowNumberer(),
		   {header : '产品名称',width : 80,dataIndex : 'name'},
		   {header : '简拼',width : 80,dataIndex : 'pinyin'},
		   {header : '产品型号',width : 40,dataIndex : 'typeCode'},
		   {header : '品牌',width : 40,dataIndex : 'brand'},
		   {header : '功率',width : 40,dataIndex : 'power'},
		   {header : '功能',width : 40,dataIndex : 'functionName'},
		   {header : '类别',width : 40,dataIndex : 'category'},
		   {header : '单位',width : 40,dataIndex : 'unit'},
		   {header : '能效',width : 40,dataIndex : 'seer'}		  
	]),

	
	getPanel:function(){
		 	 var store=new Ext.data.Store({
				 url : app.url+'/admin/productList.do',
				 baseParams:{start:0,limit:20},
				 autoLoad : true,
                 reader: new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, this.productRecord)
             });	
       		 
       		 var product_detailForm =new Ext.FormPanel({
                		border : true,
                		labelAlign : 'right',
                		labelWidth : 60,
						frame : true,
						height: 190,
                		items : [{
									layout:'column',
									items:[{
            			              xtype:'hidden',
            			              name:'id'			              			
            			            },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [{xtype : 'textfield',fieldLabel : '产品名称',name : 'name',anchor : '100%',allowBlank : false, minLength : 2,maxLength : 30}]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [{xtype : 'textfield',fieldLabel : '产品型号',name : 'typeCode',anchor : '100%',allowBlank : false, minLength : 2,maxLength : 30}]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getUnitCombo()]
								    }]
							},{// 2 row
									layout:'column',
									items:[{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getBrandCombo()]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getPowerCombo()]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getFunctionCombo()]
								    }]
							},{// 3 row
									layout:'column',
									items:[{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getSeerCombo()]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getCategoryCombo()]
								    }]
							}]
						});

       		 
       		 var addOrEditProduct_win = new Ext.Window({
       		 	layout:'fit',
		    	width:630,
		    	closeAction:'hide',
		   		height:200,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[product_detailForm],
				buttonAlign : 'center',
				buttons:[{
                			     	id:'commitButton',
									iconCls : 'save',
                			        handler:submitForm
                			       },{
                			     	 id:'closeButton',
									 text : '关闭',
						             iconCls : 'close',
                			     	 handler:function(){				     		
                			     	    addOrEditProduct_win.hide();
                			     	}
                			      }]
       		 });
       		 
       		 var product_gridPanel =  new Ext.grid.GridPanel({					  
                        border:false,
						columnLines :true,
                        enableHdMenu : false,
						loadMask : {msg : '数据加载中...'},
                        ds: store,
                        cm: this.cm_product,						
                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'增加产品',
                            iconCls:'add',
                            handler:addProduct
                        }, '-', {
                            text:'修改',
                            tooltip:'修改产品',
                            iconCls:'option',
                            handler:editProduct
                        },'-',{
                            text:'删除',
                            tooltip:'删除产品',
                            iconCls:'remove'
                        }],
						bbar: new Ext.PagingToolbar({
                            pageSize: 20,
                            store: store,
                            displayInfo: true,
                            plugins: new Ext.ux.ProgressBarPager()
                        })
            });
                      
            
            
              //取得选中的记录
	 		function getSelectedItem(){
	     		var record= product_gridPanel.getSelectionModel().getSelected();	   
	     		if(record == null){
	        		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
	         		return;
	     		}else{
	        		return record;
	     		}
	  		}
            
            function addProduct(){
            	if(product_detailForm.form.el){
            		product_detailForm.form.el.dom.reset();            	
            	}else{
            		product_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		        addOrEditProduct_win.isAdd = true;
		        addOrEditProduct_win.setTitle("新增产品");
		        addOrEditProduct_win.show();
            }
            
            function editProduct(){
            	if(product_detailForm.form.el){
            		product_detailForm.form.el.dom.reset();            	
            	}else{
            		product_detailForm.form.reset();
            	}
            	var record=getSelectedItem();
            	if(record){
            		Ext.getCmp('commitButton').setText('修改');
		    		addOrEditProduct_win.setTitle("编辑产品");
		    		addOrEditProduct_win.isAdd = false;
		    		product_detailForm.getForm().loadRecord(record);
		    		addOrEditProduct_win.show();
            	}else{
            		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
            	}           	
            }
            
            function submitForm(){
			    var form=product_detailForm.getForm();
            	if(!form.isValid()){
	         		 return;
	      		}
	      		if(addOrEditProduct_win.isAdd){
				    form.findField('id').setValue(0);
	      		   	Ext.Ajax.request({
	           			url:app.url+'/admin/createProduct.do',
	           			method :"post",
	           			jsonData :Ext.encode(form.getValues()),
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
							store.reload();
							Ext.MessageBox.alert("提示","操作成功!");
							addOrEditProduct_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
	      		}else{
            		Ext.Ajax.request({
	           			url:app.url+'/admin/updateProduct.do',
	           			method :"post",
	           			jsonData :Ext.encode(form.getValues()),
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
							store.reload();
							Ext.MessageBox.alert("提示","修改成功!");
							addOrEditProduct_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
           		}
         	}
         	
         	return product_gridPanel;
      },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('product-win');
        if(!win){      
            win = desktop.createWindow({
                id: 'brand-win',
                title:'产品管理',
                width:app.maxWidth,
                height:app.maxHeight,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

                layout: 'fit',
                items:this.getPanel()
            });
        }
        win.show();
    }
	
});